//
//  GetOrderRequest.m
//  fashionbuyer
//
//  Created by ale tan on 2018/1/16.
//  Copyright © 2018年 ale tan. All rights reserved.
//

#import "GetOrderRequest.h"

@implementation GetOrderRequest

- (id)init{
    self = [super init];
    if (self) {
        self.createOrderUrl = @"order/v1/create";
        self.createSetterUrl = @"order/v1/settle";
    }
    return self;
}

- (void)createSetterWithProducts:(NSArray *)products Address:(FBAddressModel *)address Shipping:(FBShippingWayModel *)shipping Complete:(RequestResult)requestResutl{
    
    self.requestCallBack = requestResutl;
    
    //商品
    [self injectProducts:products];
    
    //地址
    if (address) {
        [self.parameters setObject:address.addressId forKey:@"addressId"];
    }
    
    //配送方式
    if (shipping) {
        [self.parameters setObject:shipping.shippingId forKey:@"shippingId"];
    }
    
    [ServiceManager postTrans:self.createSetterUrl withRequest:self.parameters success:^(NSDictionary *dictResponse, BOOL result) {
        
        if (result) {
            NSDictionary *productDic = dictResponse[@"data"];
            FBOrderModel *model = [[FBOrderModel alloc]initWithDictionary:productDic];
            self.requestCallBack(model, YES);
        }
        else{
            [FFTipsView showTips:dictResponse[@"toast"]];
            self.requestCallBack(dictResponse,NO);
        }
        
    } failure:^(NSError *error) {
        [FFTipsView showTips:@"网络连接失败"];
        self.requestCallBack(nil,NO);
    }];
}

//下单
- (void)createOrderWithModel:(FBOrderModel *)model Complete:(RequestResult)requestResutl{
    self.requestCallBack = requestResutl;
    
    //商品
    [self injectProducts:model.products];
    
    //地址
    if (model.address) {
        [self.parameters setObject:model.address.addressId forKey:@"addressId"];
    }
    
    //配送方式
    if (model.shipping) {
        [self.parameters setObject:model.shipping.shippingId forKey:@"shippingId"];
    }
    
    //金额
    if (model.prices[@"amountPayable"]) {
        [self.parameters setObject:model.prices[@"amountPayable"] forKey:@"amountPayable"];
    }
    
    [ServiceManager postTrans:self.createOrderUrl withRequest:self.parameters success:^(NSDictionary *dictResponse, BOOL result) {
        
        if (result) {
            NSDictionary *productDic = dictResponse[@"data"];
            NSString *orderNo = productDic[@"orderNo"];
            NSString *expireTime = productDic[@"expireTime"];
            model.orderNo = orderNo;
            model.expireTime = expireTime;
            self.requestCallBack(model, YES);
        }
        else{
            [FFTipsView showTips:dictResponse[@"toast"]];
            self.requestCallBack(dictResponse,NO);
        }
        
    } failure:^(NSError *error) {
        [FFTipsView showTips:@"网络连接失败"];
        self.requestCallBack(nil,NO);
    }];
    
}

#pragma mark -Handle-
//将商品信息注入参数
- (void)injectProducts:(NSArray *)products{
    NSMutableArray *productsArray = [NSMutableArray array];
    for (FBProductModel *product in products) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:product.productId forKey:@"productId"];
        
        //skuId
        if (product.productSkuId) {
            [dic setObject:product.productSkuId forKey:@"productSkuId"];
        }
        else{
            FBSkuColorModel *colorModel = [product.skus firstObject];
            FBSkuSizeModel *sizeModel = [colorModel.xsizes firstObject];
            [dic setObject:sizeModel.productSkuId forKey:@"productSkuId"];
        }
        
        //数量
        if (product.productQty) {
            [dic setObject:product.productQty forKey:@"productQty"];
        }
        else{
            [dic setObject:@"1" forKey:@"productQty"];
        }
        
        [productsArray addObject:dic];
    }
    [self.parameters setObject:productsArray forKey:@"products"];
}

@end
